package com.example.controller;

import com.example.entity.Employee;
import com.example.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @date 2020/8/13 18:56
 */
@Controller("employeeController")
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/findall")
    @ResponseBody
    /*查询所有*/
    public List<Employee> findAll(){
        return employeeService.getAll();
    }
    @GetMapping("/findone/{id}")
    @ResponseBody
    public Employee findOne(@PathVariable("id") Integer id){
       return employeeService.get(id);
    }

    /*添加员工*/
    @PostMapping("/add")
    @ResponseBody
    public void add(@Valid Employee employee){

        /*需要进行数据校验，再进行操作*/
        Integer age = 18;
        String name = "DW";
        String sex = "男";
        String address = "上海";
        employee.setAge(age);
        employee.setName(name);
        employee.setSex(sex);
        employee.setAddress(address);
        employeeService.add(employee);
    }
    /*修改员工*/
    @PutMapping("/edit")
    @ResponseBody
    public void Edit(@Valid Employee employee){
        /*需要进行数据校验*/
        employeeService.edit(employee);
    }
    /*删除一个*/
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public void delete(@PathVariable("id") Integer id){
        employeeService.remove(id);
    }

}
